<?php

declare (strict_types = 1);

namespace app\service\admin\file;

use own\enum\FileEnum;
use think\facade\Config;

class UploadService
{
    /**
     * 单文件处理
     * @param File $file
     * @param string $type
     */
    public function upload($file, string $type): array
    {   
        if(!$file->getOriginalName()) {
            tips('文件上传失败');
        }
        if($file->getSize() > 10 * 1048576) {
            tips('文件大小不能超过10M');
        }
        //后缀名
        $exts = strtolower(strstr($file->getOriginalName(),'.'));
        //文件存储路径
        list($path, $url) = make(FileEnum::class)->storageName($exts, $type);
        //保存
        if (move_uploaded_file($file->getRealPath(), $path)) {
            return ['url' => $url, 'access_url' => Config::get('filesystem.access_url') . $url];
        }
        tips('文件上传失败');
    }
}